import Vue from 'vue'
import Router from 'vue-router'
import Index from './views/Index.vue'
import Login from './views/Login.vue'
import Register from './views/Register.vue'
import NotFound from './views/Not404.vue'
import Home from './views/Home.vue'
import Menu from './views/Menu.vue'
import Oldpep from './views/Oldpep.vue'
import Share from './views/Share.vue'
import Contact from './views/Contact.vue'
import Personal from './views/Personal.vue'
import Edit from './views/Edit.vue'
import Upmenu from './views/Upmenu.vue'
import Story from './views/Story.vue'
import Published from './views/Published.vue'
import Collect from './views/Collect.vue'
import Exchange from './views/Exchange.vue'
import Top from './views/Top.vue'
import Article from './views/Article.vue'
import ShareDetail from './views/Sharedetail.vue'
import Collectidone from './views/Collectidone.vue'
import Collectidtwo from './views/Collectidtwo.vue'
import Collectidzero from './views/Collectidzero.vue'
import UpdHeadPic from './views/UpdHeadPic.vue'
import Search from './views/Search.vue'

Vue.use(Router)
let router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: '/index/home' //路由跳转
    },
    {
      path: '/index',
      name: 'index',
      component: Index,
      children: [
        {
          path: 'home',
          name: 'home',
          component: Home
        },
        {
          path: 'menu',
          name: 'menu',
          component: Menu,
        },
        {
          path: 'top/mealId/:mealId',
          name: 'top',
          component: Top
        },
        {
          path: 'article/:peopleId',
          name: 'article',
          component: Article
        },
        {
          path: 'oldpep',
          name: 'oldpep',
          component: Oldpep
        },
        {
          path: 'share',
          name: 'share',
          component: Share
        },
        {
          path: 'sharedetail/:upmenuId',
          name: 'sharedetail',
          component: ShareDetail
        },
        {
          path: 'contact',
          name: 'contact',
          component: Contact
        },
        {
          path: 'search/:searchcontent',
          name: 'search',
          component: Search
        },
      ]
    },
    {
      path: '/personal',
      redirect: '/personal/story' //路由跳转
    },
    {
      path: '/personal',
      name: 'personal',
      component: Personal,
      children: [
        {
          path: 'updheadpic',
          name: 'UpdHeadPic',
          component: UpdHeadPic
        },
        {
          path: 'story',
          name: 'story',
          component: Story
        },
        {
          path: 'edit',
          name: 'edit',
          component: Edit
        },
        {
          path: 'upmenu',
          name: 'upmenu',
          component: Upmenu
        },
        {
          path: 'published',
          name: 'published',
          component: Published
        },
        {
          path: 'collect',
          name: 'collect',
          component: Collect,
          children: [
            {
              path: 'collectid0',
              name: 'collectid0',
              component: Collectidzero
            },
            {
              path: 'collectid1',
              name: 'collectid1',
              component: Collectidone
            },
            {
              path: 'collectid2',
              name: 'collectid2',
              component: Collectidtwo
            },
          ]
        },
        {
          path: 'exchange',
          name: 'exchange',
          component: Exchange
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/register',
      name: 'register',
      component: Register
    },
    {
      path: '/*',   //路由匹配不成功时
      name: 'notfound',
      component: NotFound
    }
  ]
})
//设置路由守卫
router.beforeEach((to,from,next) => {
  //除了login和register，其他的路由访问必须先登录
  let tokenIsExists = localStorage.getItem('mytoken') ? true : false //检查本地存储中是否有token
  if(to.path == '/login' || to.path == '/register'){
    next()    //允许访问路由
  }else{
    if(tokenIsExists){
      next()  //已经登录并取得token，允许访问路由
    }else{
      next('/login')  //路由跳转到登录组件
    }
  }
})
export default router
